//
//  ModelTest.m
//  Hello_OCUnit
//
//  Created by zhudongxia on 15/12/11.
//  Copyright © 2015年 zhudongxia. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "Counter.h"
#import "OCMock.h"
#define HC_SHORTHAND
#import <OCHamcrestIOS/OCHamcrestIOS.h>
//#define MOCKITO_SHORTHAND
//#import <OCMockitoIOS/OCMockitoIOS.h>


@interface ModelTest : XCTestCase
{
    Counter * sut;
    NSUserDefaults *mockDefaults;
    int modelChangedCount;
    NSInteger modelChangedValue;
    
}
@end

@implementation ModelTest

- (void)setUp {
    [super setUp];
    self.continueAfterFailure = NO;
    mockDefaults = OCMClassMock([NSUserDefaults class]);
    sut = [[Counter alloc] initWithUserDefault:mockDefaults ];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(counterModelChanged:) name:CounterModelChanged object:sut];
    
}

# pragma  mark 测试 init方法 是否可以成功创建  实例非空即为成功
-(void)testInitShouldNotReturenNil
{
    Counter *counter = [[Counter alloc]initWithUserDefault:[NSUserDefaults standardUserDefaults]];
    assertThat(counter,instanceOf([Counter class]));
    
}
# pragma  mark 测试 mockDefaults中数量为空时 是否可以返回0
-(void) testGetCountInDefaultsWithNilShouldReturnZero
{
    OCMStub([mockDefaults objectForKey:@"currentId"]).andReturn(nil);
    
    assertThatInteger([sut getCountInDefaults], equalToInteger(0));
    
}

# pragma  mark 测试 mockDefaults中数量为  3 时 是否可以返回 3
-(void) testGetCountInDefaultsWithNumberThreeShouldReturnIntegerThree
{
   OCMStub([mockDefaults objectForKey:@"currentId"]).andReturn(@3);
    assertThatInteger([sut getCountInDefaults], equalToInteger(3));
}

# pragma  mark 测试 setObject 是否被成功调用
-(void) testIncrementShouldInvokeSetObject
{
    OCMStub([mockDefaults objectForKey:@"currentId"]).andReturn(@3);
    [sut increment];
    OCMVerify([mockDefaults setObject:@4 forKey:@"currentId"]);
}

# pragma  mark 测试 是否成功发送通知
-(void) testIncrementShouldPostNotification
{
    OCMStub([mockDefaults objectForKey:@"currentId"]).andReturn(@3);
    [sut increment];
    assertThatInt(modelChangedCount, is(equalToInt(1)));
    assertThatInteger(modelChangedValue, is(equalToInteger(4)));
    
}

# pragma  mark 收到通知
-(void) counterModelChanged:(NSNotification *) notification
{
    ++modelChangedCount;
    Counter *counter = (Counter *) [notification object];
    modelChangedValue = [counter count];

}


- (void)tearDown {
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    sut = nil;
    [super tearDown];
}



@end
